<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	protected $id;
	protected $tag;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		One::model();
		$criteria=new CDbCriteria();
		$criteria->addCondition('username = :username');
		$criteria->params = array(
			':username' => $this->username,
		);
		$oneExt = OneExt::model()->find($criteria);
		if ($oneExt === NULL) {
			$this->errorCode=self::ERROR_USERNAME_INVALID;
			return !$this->errorCode;
		}
		$one = One::model()->findByPk($oneExt->id);
		$pw = $one->get("password");
		if ($pw === NULL) {
			$this->errorCode=self::ERROR_USERNAME_INVALID;
			return !$this->errorCode;
		}
		if ($pw !== md5($this->password) ) {
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
			return !$this->errorCode;
		}
		$this->id  = $one->id;
		$this->tag = $one->tag;
		$this->setState("id",$this->getId());
		$this->setState("type",$one->type);
		$this->setState("detail",$one->getDetail());
		$this->errorCode=self::ERROR_NONE;
		return !$this->errorCode;
	}
	/**
	 * Returns the unique identifier for the identity.
	 * The default implementation simply returns {@link username}.
	 * This method is required by {@link IUserIdentity}.
	 * @return string the unique identifier for the identity.
	 */
	public function getId()
	{
		return $this->id;
	}   

	/**
	 * Returns the display name for the identity.
	 * The default implementation simply returns {@link username}.
	 * This method is required by {@link IUserIdentity}.
	 * @return string the display name for the identity.
	 */
	public function getName()
	{
		return $this->tag;
	}
}
